package com.shao.commons.domain;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

@Data
@Accessors(chain = true)
public class R<T> implements Serializable {

    private Integer code;
    private String message;
    private T data;

    public static <S> R<S> success(S data) {
        return new R()
                .setCode(Codes.SUCCESS.getCode())
                .setMessage(Codes.SUCCESS.getMsg())
                .setData(data);
    }

    public static R success() {
        return success(null);
    }

    public static <S> R<S> fail(Codes codes, S data) {
        return new R()
                .setCode(codes.getCode())
                .setMessage(codes.getMsg())
                .setData(data);
    }

    public static R fail(Codes codes) {
        return fail(codes, null);
    }
}
